package com.zonski.jbots.desktop.editer;

import com.zonski.jbots.engine.*;

import javax.swing.*;
import java.awt.*;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Chris Glover
 * Date: Oct 27, 2003
 * Time: 9:19:05 AM
 * To change this template use Options | File Templates.
 */
public class JBotsEditerContext
{
    private Component component;
    private ToolManager toolManager;
    private Engine engine;
    private EditerOperations operations;

    private int currentLevel;
    private boolean centredOnTile = true;
    private JComboBox layerCombo;

    public JBotsEditerContext(Component component, Engine engine, JComboBox layerCombo)
    {
        this.component = component;
        this.layerCombo = layerCombo;
        this.engine = engine;
        this.toolManager = new ToolManager(this);
    }

    public Room getRoom()
    {
        return this.engine.getRoom();
    }

    public void setRoom(Room room)
    {
        this.engine.setRoom(room);
    }

    /**
     * obtains the component that the room is being rendered upon
     * @return the component that the room is being rendered upon
     */
    public Component getComponent()
    {
        return this.component;
    }

    public ToolManager getToolManager()
    {
        return this.toolManager;
    }

    public EntityFactory getEntityFactory()
    {
        return this.engine.getEntityFactory();
    }

    public boolean isCentredOnTile()
    {
        return this.centredOnTile;
    }

    public void setCentredOnTile(boolean centredOnTile)
    {
        this.centredOnTile = centredOnTile;
    }

    public int getCurrentLevel()
    {
        return this.currentLevel;
    }

    public void setCurrentLevel(int currentLevel)
    {
        if(currentLevel >= 0 && currentLevel != this.currentLevel)
        {
            this.currentLevel = currentLevel;
            this.layerCombo.setSelectedItem(Integer.toString(currentLevel));
        }
    }

    public void showError(String message, Exception ex)
    {
        this.operations.showError(message, ex);
    }

    public void setOperations(EditerOperations operations)
    {
        this.operations = operations;
    }

    public Entity getEntityAt(int x, int y)
    {
        Entity entity = null;
        Room room = this.getRoom();
        QuickVector layers = room.getLayers();
        outer: for(int i=0; i<layers.size(); i++)
        {
            Layer layer = (Layer)layers.elementAt(i);
            QuickVector entities = layer.getEntities();
            for(int j=0; j<entities.size(); j++)
            {
                Entity found = (Entity)entities.elementAt(j);
                if(found.bounds.contains(x, y))
                {
                    entity = found;
                    break outer;
                }
            }
        }
        return entity;
    }
}
